/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.support;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.env.Environment;

/**
 * 
 *
 * @author haolw
 * @time 2016/10/10
 */
public class BeanContext implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	public static Environment getEnvironment() {
		return applicationContext.getEnvironment();
	}

	public static <T> T getBean(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}

	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}

	public static <T> T getBean(String name, Class<T> clazz) {
		return applicationContext.getBean(name, clazz);
	}

	public static String[] getBeanNames() {
		return applicationContext.getBeanDefinitionNames();
	}

	public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
		return applicationContext.getBeansOfType(clazz);
	}

	public static void publishEvent(ApplicationEvent event) {
		applicationContext.publishEvent(event);
	}

	public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotation) {
		return applicationContext.getBeansWithAnnotation(annotation);
	}

	public static <A extends Annotation> A findAnnotationOnBean(String name, Class<A> annotation) {
		return applicationContext.findAnnotationOnBean(name, annotation);
	}

	public void setApplicationContext(ApplicationContext context) throws BeansException {
		applicationContext = context;
	}
}
